/*
 * Simple test file for cxxomfort
 * When compiled and run, this will print information about the
 * C++ and cxxomfort install, and what SD6 / feature macros have been detected or defined by the compiler or by the library.
 * 
 */
#include <cxxomfort/base.hpp>
#include <cxxomfort/backports.hpp>
#include <cxxomfort/library/type_name.hpp>
#include <list>
#include <iostream>
#include <cstdio> // for snprintf

/*
 * BEGIN SECTION
 * Uncomment these as desired, if you have the required cxxomfort extensions
 * Alternatively, add here any third party that provides or facades the functionality and announces it as defined by the C++ Standard (ie.: via the required __cpp_lib_ macros).
 * 
 * */

// from cxxomfort:vocab17
//#include <cxxomfort/any.hpp>
//#include <cxxomfort/optional.hpp>
//#include <cxxomfort/variant.hpp>
// from cxxomfort:minranges
//#include <cxxomfort/ranges.hpp>

/*
 * END SECTION
 * */
#if (CXXOMFORT_CXX_STD>=2020)
    #include <version>
#endif

#define CCHECK_HELPER(...) (#__VA_ARGS__[0] == '1' || #__VA_ARGS__[0] == '\0')
#define CCHECK(...) CCHECK_HELPER(__VA_ARGS__)

#define CXXOSHOWSUPPORT(macro) (CXXO_IF___cpp_feature(macro) ? ("" #macro " : " CXXO_EXPAND(CXXO_STRINGIZE(macro) )  "\n") : "")

void show_sd6_feature (std::ostream&);
void show_sd6_library (std::ostream&);

std::array<int,5> arr = {{5, 4, 3, 2, 1}};
std::list<int> lst;

int main () {
    using std::cout;
    using std::endl;
    // for container related tests
    (void)arr;
    (void)lst;
    cxxomfort::output_info(stdout);
    cout<< endl;
    cout<< "SD6 macros:\n";
    cout<< "C++ Compiler Feature:\n";
    show_sd6_feature(cout);
    cout<< "C++ Library Feature:\n";
    show_sd6_library(cout);
    
    cout<< "\n"<< endl;
    
    cout<< "\nC++20 lib:"<< endl;
    cout<< CXXOSHOWSUPPORT(__cpp_lib_atomic_ref);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_bind_front);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_bit_cast);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_bitops);
    #if (defined(__cpp_lib_bitops)) 
    {
	cout<< "test bit_floor, bit_ceil: "<< std::flush;
	uint32_t const x = 0x00123400;
	cout<< x<< " \t"<< std::bit_floor(x)<< " "<< std::bit_ceil(x)<< endl;
    }
    #endif
    cout<< CXXOSHOWSUPPORT(__cpp_lib_bounded_array_traits);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_constexpr_algorithms);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_constexpr_numeric);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_constexpr_string_view);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_destroying_delete);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_endian);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_erase_if);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_int_pow2);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_integer_comparison_functions);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_interpolate);
    #if (defined(__cpp_lib_interpolate))
    {
	cout<< "Test midpoint: "<< std::midpoint(SHRT_MIN, SHRT_MAX)<< endl;
    }
    #endif
    cout<< CXXOSHOWSUPPORT(__cpp_lib_remove_cvref);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_shift);
    #if (defined(__cpp_lib_shift))
    {
	cout<< "test on shift:"<< endl;
	std::array<int,5> arr2= arr;
	CXXO_FOREACH(int a, arr2) { cout<< a<< "\t"; } cout<< endl;
	std::shift_right(arr2.begin(), arr2.end(), 2);
	CXXO_FOREACH(int a, arr2) { cout<< a<< "\t"; } cout<< endl;
	std::shift_left(arr2.begin(), arr2.end(), 2);
	CXXO_FOREACH(int a, arr2) { cout<< a<< "\t"; } cout<< endl;
    }
    #endif
    cout<< CXXOSHOWSUPPORT(__cpp_lib_span);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_ssize);
    #if (defined(__cpp_lib_ssize))
    {
	cout<< "test on array: "<< std::ssize(arr)<< endl;
    }
    #endif
    cout<< CXXOSHOWSUPPORT(__cpp_lib_starts_ends_with);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_to_array);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_type_identity);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_unwrap_ref);
    cout<< "\nC++23 lib:"<< endl;
    cout<< CXXOSHOWSUPPORT(__cpp_lib_bind_back);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_byteswap);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_expected);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_invoke_r);
    #if (defined(__cpp_lib_invoke_r))
    {
	int test = std::invoke_r<int>(std::snprintf, nullptr, 0, "Number %03d.", 5);
	cout<< "test of invoke_r: "<< test<< " characters needed."<< endl;
    }
    #endif
    cout<< CXXOSHOWSUPPORT(__cpp_lib_is_scoped_enum);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_string_contains);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_to_underlying);
    cout<< CXXOSHOWSUPPORT(__cpp_lib_unreachable);
    cout<< "\nC++2y / C++26? lib:"<< endl;
    cout<< "\n"<< endl;

}

void show_sd6_feature (std::ostream& os) {
    using namespace std;
    os<< "-- Begin SD6 feature macros"<< endl;
    os<< "C++ general:"<< endl;
    os<< CXXOSHOWSUPPORT(__cpp_exceptions);
    os<< CXXOSHOWSUPPORT(__cpp_rtti);
    os<< CXXOSHOWSUPPORT(_EXCEPTIONS);
    os<< "C++11:"<< endl;
    os<< CXXOSHOWSUPPORT(__cpp_attributes);
    os<< CXXOSHOWSUPPORT(__cpp_constexpr);
    os<< CXXOSHOWSUPPORT(__cpp_decltype);
    os<< CXXOSHOWSUPPORT(__cpp_delegating_constructors);
    os<< CXXOSHOWSUPPORT(__cpp_inheriting_constructors);
    os<< CXXOSHOWSUPPORT(__cpp_lambdas);
    os<< CXXOSHOWSUPPORT(__cpp_range_based_for);
    os<< CXXOSHOWSUPPORT(__cpp_ref_qualifiers);
    os<< CXXOSHOWSUPPORT(__cpp_rvalue_references);
    os<< CXXOSHOWSUPPORT(__cpp_static_assert);
    os<< CXXOSHOWSUPPORT(__cpp_unicode_characters); // char16_t etc
    os<< CXXOSHOWSUPPORT(__cpp_unicode_literals);
    os<< CXXOSHOWSUPPORT(__cpp_user_defined_literals);
    os<< CXXOSHOWSUPPORT(__cpp_variadic_templates);
    os<< "C++14:"<< endl;
    os<< CXXOSHOWSUPPORT(__cpp_decltype_auto);
    os<< CXXOSHOWSUPPORT(__cpp_generic_lambdas);
    os<< CXXOSHOWSUPPORT(__cpp_return_type_deduction);
    os<< CXXOSHOWSUPPORT(__cpp_variable_templates);
    os<< "C++17:"<< endl;
    os<< CXXOSHOWSUPPORT(__cpp_inline_variables);
    os<< CXXOSHOWSUPPORT(__cpp_noexcept_function_type);
    os<< CXXOSHOWSUPPORT(__cpp_structured_bindings);
    os<< "C++20:"<< endl;
    os<< CXXOSHOWSUPPORT(__cpp_conditional_explicit);
    os<< "C++23:"<< endl;
    os<< CXXOSHOWSUPPORT(__cpp_size_t_suffix);
    os<< "C++26?:"<< endl;
    os<< "-- Done"<< endl;
}

void show_sd6_library (std::ostream& os) {
    using std::endl;
    os<< "\nC++14 lib:"<< endl;
    os<< CXXOSHOWSUPPORT(__cpp_lib_complex_udls);
    #if (defined(__cpp_lib_complex_udls)) 
    {
	using namespace std::literals::complex_literals;
	std::complex<float> test = 1.2f-3.4if;
	os<< test<< endl;
    }
    #endif
    os<< CXXOSHOWSUPPORT(__cpp_lib_exchange_function);
    os<< CXXOSHOWSUPPORT(__cpp_lib_integer_sequence);
    os<< CXXOSHOWSUPPORT(__cpp_lib_integral_constant_callable);
    os<< CXXOSHOWSUPPORT(__cpp_lib_make_reverse_iterator);
    os<< CXXOSHOWSUPPORT(__cpp_lib_make_unique );
    #if (defined(__cpp_lib_make_unique)) 
    {
	int p= *std::make_unique<int>(4);
	os<< "test: "<< p<< endl;
    }
    #endif
    os<< CXXOSHOWSUPPORT(__cpp_lib_quoted_string_io);
    #if (defined(__cpp_lib_quoted_string_io)) 
    {
	os<< "Test quoted: "<< std::quoted("Hello \"World\"!", '\'', '@')<< endl;
    }
    #endif
    os<< CXXOSHOWSUPPORT(__cpp_lib_robust_nonmodifying_seq_ops);
    os<< CXXOSHOWSUPPORT(__cpp_lib_string_udls);
    #if (defined(__cpp_lib_string_udls))
    {
	using namespace std::literals::string_literals;
	os<< "test UDL \"\"s: "<< "Test"s<< endl;
    }
    #endif
    os<< CXXOSHOWSUPPORT(__cpp_lib_transparent_operators);
    #if (defined(__cpp_lib_transparent_operators))
    {
	std::plus<void> P;
	os<< "test: "<< P(40,2)<< endl;
    }
    #endif
    os<< CXXOSHOWSUPPORT(__cpp_lib_tuples_by_type);
    
    os<< "\nC++17 lib:"<< endl;
    os<< CXXOSHOWSUPPORT(__cpp_lib_any);
    os<< CXXOSHOWSUPPORT(__cpp_lib_apply);
    os<< CXXOSHOWSUPPORT(__cpp_lib_as_const);
    os<< CXXOSHOWSUPPORT(__cpp_lib_bool_constant);
    #if (defined(__cpp_lib_bool_constant))
    {
	using cxxomfort::fix::to_value;
	os<< "test bool_constant<bool>: "<< std::flush;
	os<< to_value(std::bool_constant<true>())
	<< " "<< to_value(std::bool_constant<false>())<< endl;
    }
    #endif
    os<< CXXOSHOWSUPPORT(__cpp_lib_byte);
    os<< CXXOSHOWSUPPORT(__cpp_lib_clamp);
    #if (defined(__cpp_lib_clamp)) 
    {
	int const z = -1;
	int const c= std::clamp(z, 0, 9); // clamp returns a potentially dangling ref
	os<< "test -1 into [0, 9]: "<< c<< endl;
    }
    #endif
    os<< CXXOSHOWSUPPORT(__cpp_lib_gcd_lcm);
    os<< CXXOSHOWSUPPORT(__cpp_lib_hypot);
    #if (defined(__cpp_lib_hypot))
    {
	os<< "test triangle hypot: "<< std::hypot(2.0, 3.0)<< endl;
    }
    #endif
    os<< CXXOSHOWSUPPORT(__cpp_lib_invoke);
    #if (defined(__cpp_lib_invoke))
    {
	os<< "test invoke printf: ";
	std::invoke(printf, "1 = %d", 1);
	os<< endl;
    }
    #endif
    os<< CXXOSHOWSUPPORT(__cpp_lib_is_aggregate);
    os<< CXXOSHOWSUPPORT(__cpp_lib_is_invocable);
    os<< CXXOSHOWSUPPORT(__cpp_lib_make_from_tuple);
    #if (defined(__cpp_lib_make_from_tuple) && CXXOMFORT_CXX_STD>=2011)
    {
	os<< "Test make string from tuple: "<< std::flush;
	auto t= std::make_tuple(4, 'h');
	auto mkt = std::make_from_tuple<std::string>(t);
	os<< cxxomfort::type_name<decltype(mkt)>()<< " : "<< mkt<< endl;
    }
    #endif
    os<< CXXOSHOWSUPPORT(__cpp_lib_nonmember_container_access);
    #if (defined(__cpp_lib_nonmember_container_access)) 
    {
	os<< "test nonmember container access on array 'arr': ";
	os<< "size "<< std::size(arr)<< " empty "<< std::empty(arr)<< " data "<< (void*)std::data(arr)<< endl;
    }
    #endif
    os<< CXXOSHOWSUPPORT(__cpp_lib_not_fn);
    os<< CXXOSHOWSUPPORT(__cpp_lib_raw_memory_algorithms );
    os<< CXXOSHOWSUPPORT(__cpp_lib_optional);
    os<< CXXOSHOWSUPPORT(__cpp_lib_string_view);
    #if (defined(__cpp_lib_string_view))
    {
	os<< "test: "<< std::string_view("Hello World!")<< endl;
    }
    #endif
    os<< CXXOSHOWSUPPORT(__cpp_lib_to_chars);
    os<< CXXOSHOWSUPPORT(__cpp_lib_variant);
    os<< CXXOSHOWSUPPORT(__cpp_lib_void_t);
}
